package com.hdoit.jiayuntong.adapter;

import java.util.List;

import com.hdoit.jiayuntong.R;
import com.hdoit.jiayuntong.data.LocalImageCacheWork;
import com.hdoit.jiayuntong.data.LocalImageHandler;
import com.hdoit.jiayuntong.entity.LocalPictureSelectedStatus;
import com.hdoit.jiayuntong.entity.Picture;
import com.ta.TAApplication;
import com.ta.util.extend.draw.DensityUtils;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class LocalImageAdapter extends BaseAdapter {
    
	private Context context;
	private LocalImageCacheWork imageFetcher;
	private List<Picture> pics;
	private int gridWidth = 0;
	
    public LocalImageAdapter(Context context,List<Picture> pics,TAApplication application) {
		super();
		this.context = context;
		
		WindowManager wm = (WindowManager)(context.getSystemService(context.WINDOW_SERVICE));
		gridWidth = wm.getDefaultDisplay().getWidth();
		LocalImageHandler bitmapFetcher = new LocalImageHandler(
				context, DensityUtils.dipTopx(context, gridWidth/3.0f),
				DensityUtils.dipTopx(context, gridWidth/3.0f));
		imageFetcher = new LocalImageCacheWork(context);
		imageFetcher.setFileCache(application.getFileCache());
		imageFetcher.setProcessDataHandler(bitmapFetcher);
		this.pics = pics;
	}
	
	@Override
	public int getCount() {
		return pics.size();
	}

	@Override
	public Object getItem(int position) {
		return pics.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder=null;
		if (convertView == null){
			convertView = LayoutInflater.from(context).inflate(R.layout.activity_picture_select_checked, null);
			holder = new ViewHolder(convertView);
		} else{
		    holder = (ViewHolder)convertView.getTag();
		}
		
		if(holder != null){
			imageFetcher.loadFormCache(pics.get(position).getFilePath(), holder.iv_photo);
			holder.bindData(pics.get(position).status);
		}
		
		return convertView;
	}
	
    class ViewHolder{
		
		public ViewHolder(View convertView)
		{
			this.convertView = convertView;
			this.iv_photo = (ImageView)convertView.findViewById(R.id.iv_photo);
			this.iv_photo.setLayoutParams(new FrameLayout.LayoutParams(gridWidth/3,gridWidth/3));
			this.iv_selected = (ImageView)convertView.findViewById(R.id.iv_selected);
			this.mask = (View)convertView.findViewById(R.id.mask);
			this.mask.setLayoutParams(new FrameLayout.LayoutParams(gridWidth/3,gridWidth/3));
			convertView.setTag(this);
		}
		
		public void bindData(LocalPictureSelectedStatus status){
			if(status == LocalPictureSelectedStatus.Selected){
				this.iv_selected.setImageResource(R.drawable.btn_selected); 
				this.mask.setVisibility(View.VISIBLE);
				//this.iv_selected.setVisibility(View.VISIBLE);
			}else if(status == LocalPictureSelectedStatus.Unselected){
				this.iv_selected.setImageResource(R.drawable.btn_unselected); 
				this.mask.setVisibility(View.GONE);
				//this.iv_selected.setVisibility(View.GONE);
			}
		}
		
		View convertView;
		ImageView iv_selected;
		ImageView iv_photo;
		View mask;
	}
}
